package com.example.badmintonUser.admin.AdminServerImpl;

import com.example.badmintonCommon.entity.Gym;
import com.example.badmintonCommon.entity.Notice;
import com.example.badmintonCommon.entity.Order;
import com.example.badmintonCommon.entity.User;
import com.example.badmintonCommon.pojo.Result;
import com.example.badmintonCommon.util.JwtUtil;
import com.example.badmintonDB.AdminDBServer;
import com.example.badmintonDB.UserDBServer;
import com.example.badmintonUser.admin.AdminServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class AdminServerImpl implements AdminServer {
    private final AdminDBServer adminDBServer;
    private final UserDBServer userDBServer;
    @Autowired
    public AdminServerImpl(AdminDBServer adminDBServer,UserDBServer userDBServer){
        this.adminDBServer=adminDBServer;
        this.userDBServer=userDBServer;
    }
    //获得所有用户信息
    @Override
    public Result<List<User>> GetUsersList(){
        List<User> userList =adminDBServer.FindAllUsers("普通用户");
        return Result.success(userList);
    }

    //获得所有预约信息
    @Override
    public Result<String> GetOdersList(){
        return Result.success();
    }

    //查询用户密码
    @Override
    public Result<String> GetUserPsw(String tel){
        return Result.success();
    }

    //添加场地信息
    @Override
    public Result<String> AddGym(Integer gymprise){

        Integer newId = generateNextGymId();
        Gym gym =new Gym();
        gym.setId(newId);
        gym.setStatus("available");
        gym.setGymprise(80);
        // 自动创建7x24的二维数组
        int[][] place = new int[7][24];
        gym.setPlace(place);

        adminDBServer.AddGym(gym);
        return Result.success("添加成功");
    }

    //生成下一个场地ID
    @Override
    public Integer generateNextGymId() {
        Integer maxId = adminDBServer.getMaxId();
        if (maxId == null) {
            return 1;  // 没有数据，从1开始
        } else {
            return maxId + 1;  // 有数据，最大ID + 1
        }
    }

    //删除场地信息
    @Override
    public Result<String> DeleteGymById(Integer id){
        adminDBServer.DeleteGymById(id);
        return Result.success("删除成功");
    }

    //增加公告
    @Override
    public void AddNotice(Notice notice) {
        adminDBServer.AddNotice(notice);
    }

    //更新公告
    @Override
    public void UpdateNotice(Notice notice) {
        Notice old = adminDBServer.FindNoticeById(notice.getId());
        if (old == null) {
            throw new RuntimeException("公告不存在");
        }
        adminDBServer.UpdateNotice(notice);
    }

    //找所有公告
    @Override
    public List<Notice> FindAllNotices() {
        return adminDBServer.FindAllNotices();
    }

    //通过id找公告
    @Override
    public Notice FindNoticeById(Integer id) {
        return adminDBServer.FindNoticeById(id);
    }

    @Override
    public Result<List<Order>> getOrders() {

        try {
            List<Order> list=adminDBServer.getOrders();
            return Result.success(list);
        }catch (Exception e){
            return Result.error();
        }

    }

    @Override
    public Result<List<Gym>> getGyms() {
        try {
            List<Gym> list=adminDBServer.getGyms();
            return Result.success(list);
        }catch (Exception e){
            return Result.error();
        }

    }

    @Override
    public Result<String> changePlacePrice(int place, int price) {
        Result result=adminDBServer.changePlacePrice(place,price);
        return result;
    }

    @Override
    public Result<String> lockOrUnlockUser(String tel) {
        Result result=adminDBServer.lockOrUnlockUser(tel);
        return result;
    }

    @Override
    public Result<String> deleteNotice(int id) {
        Result result=adminDBServer.deleteNotice(id);
        return result;
    }


    //用户通过手机号登录
    @Override
    public Result<String> AdminLogin(String tel, String psw) {
        if (!userDBServer.existsByUserTel(tel)) {

            return Result.error("不存在该用户");
        }

        // 从 DB 取出已加密的密码（BCrypt 格式）
        String encodedPsw = userDBServer.getPasswordByTel(tel); // <-- 需保证此方法存在并返回加密后的密码
        String role = userDBServer.getRoleByTel(tel);
        if (Objects.equals(role, "普通用户")) {
            return Result.error("用户不存在");
        }
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        if (!encoder.matches(psw, encodedPsw)) {

            return Result.error("密码错误");
        }

        // 验证通过，生成 token 返回
        Map<String, Object> claims = new HashMap<>();
        claims.put("tel", tel);
        String token = JwtUtil.genToken(claims);
        return Result.success(token);
    }
}
